/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on 28.feb.2011, 20:03:58
 */
package EvoAlg;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author DiaAWAY
 */
public class GUI extends javax.swing.JFrame {

    private IzhiVerifier verifier = new IzhiVerifier();
    private Graph graphTarget;
    private Graph graphGenerated;

    /** Creates new form GUI */
    public GUI() {
        initComponents();
        graphTarget = new Graph();
        graphGenerated = new Graph();
        graphTarget.setMin(new double[0]);
        graphTarget.setMax(new double[0]);
        graphTarget.setAvg(new double[0]);
        graphTarget.setxSize(0);
        graphTarget.setySize(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        RawData = new javax.swing.JPanel();
        Target = new javax.swing.JPanel();
        labelGraphGenerated = new javax.swing.JLabel();
        Generated = new javax.swing.JPanel();
        labelGraphTarget = new javax.swing.JLabel();
        Meny = new javax.swing.JPanel();
        Variables = new javax.swing.JPanel();
        labelPopulation = new javax.swing.JLabel();
        textPopulation = new javax.swing.JTextField();
        labelMutation = new javax.swing.JLabel();
        textMutation = new javax.swing.JTextField();
        labelCrossover = new javax.swing.JLabel();
        textCrossover = new javax.swing.JTextField();
        btnStart = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Exercise 2");
        setName("Main"); // NOI18N
        getContentPane().setLayout(new java.awt.BorderLayout(5, 5));

        RawData.setLayout(new java.awt.BorderLayout(5, 5));

        Target.setBackground(new java.awt.Color(255, 255, 255));
        Target.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        Target.setPreferredSize(new java.awt.Dimension(250, 300));
        Target.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        labelGraphGenerated.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        labelGraphGenerated.setText("Generated");
        labelGraphGenerated.setMaximumSize(new java.awt.Dimension(70, 14));
        labelGraphGenerated.setMinimumSize(new java.awt.Dimension(70, 14));
        labelGraphGenerated.setPreferredSize(new java.awt.Dimension(70, 14));
        Target.add(labelGraphGenerated);

        RawData.add(Target, java.awt.BorderLayout.LINE_START);

        Generated.setBackground(new java.awt.Color(255, 255, 255));
        Generated.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        Generated.setPreferredSize(new java.awt.Dimension(250, 300));
        Generated.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        labelGraphTarget.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        labelGraphTarget.setText("Target");
        labelGraphTarget.setMaximumSize(new java.awt.Dimension(40, 14));
        labelGraphTarget.setMinimumSize(new java.awt.Dimension(40, 14));
        labelGraphTarget.setPreferredSize(new java.awt.Dimension(40, 14));
        Generated.add(labelGraphTarget);

        RawData.add(Generated, java.awt.BorderLayout.LINE_END);

        getContentPane().add(RawData, java.awt.BorderLayout.EAST);

        Meny.setPreferredSize(new java.awt.Dimension(150, 100));
        Meny.setLayout(new java.awt.BorderLayout(5, 5));

        Variables.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        labelPopulation.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        labelPopulation.setText("Population:");
        Variables.add(labelPopulation);

        textPopulation.setInputVerifier(verifier);
        textPopulation.setMinimumSize(new java.awt.Dimension(59, 20));
        textPopulation.setPreferredSize(new java.awt.Dimension(59, 20));
        Variables.add(textPopulation);

        labelMutation.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        labelMutation.setText("Mutation:");
        Variables.add(labelMutation);

        textMutation.setInputVerifier(verifier);
        textMutation.setMinimumSize(new java.awt.Dimension(59, 20));
        textMutation.setPreferredSize(new java.awt.Dimension(59, 20));
        Variables.add(textMutation);

        labelCrossover.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        labelCrossover.setText("Crossover:");
        Variables.add(labelCrossover);

        textCrossover.setInputVerifier(verifier);
        textCrossover.setMinimumSize(new java.awt.Dimension(59, 20));
        textCrossover.setPreferredSize(new java.awt.Dimension(59, 20));
        Variables.add(textCrossover);

        Meny.add(Variables, java.awt.BorderLayout.CENTER);

        btnStart.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        btnStart.setText("Start!");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });
        Meny.add(btnStart, java.awt.BorderLayout.SOUTH);

        getContentPane().add(Meny, java.awt.BorderLayout.WEST);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        // we're using a verifier for the textfields, so we can assume that they are safe to use
    }//GEN-LAST:event_btnStartActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUI g = new GUI();
                g.Target.add(g.graphTarget);
                g.Generated.add(g.graphGenerated);
                g.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Generated;
    private javax.swing.JPanel Meny;
    private javax.swing.JPanel RawData;
    private javax.swing.JPanel Target;
    private javax.swing.JPanel Variables;
    private javax.swing.JButton btnStart;
    private javax.swing.JLabel labelCrossover;
    private javax.swing.JLabel labelGraphGenerated;
    private javax.swing.JLabel labelGraphTarget;
    private javax.swing.JLabel labelMutation;
    private javax.swing.JLabel labelPopulation;
    private javax.swing.JTextField textCrossover;
    private javax.swing.JTextField textMutation;
    private javax.swing.JTextField textPopulation;
    // End of variables declaration//GEN-END:variables
}

class IzhiVerifier extends InputVerifier {

    @Override
    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;
        double value = 0.0;
        boolean pass = false;
        try {
            value = Double.parseDouble(tf.getText());
            if (value > 0) {
                pass = true;
            } else {
                JOptionPane.showMessageDialog(tf.getTopLevelAncestor(), "Must be a positive value above zero.",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(tf.getTopLevelAncestor(), "Use only valid numbers.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
        return pass;
    }
}
